import { Controller } from 'egg';

export default class SocketController extends Controller {
  public async init() {
    try {
      // 初始化 Socket.io 服务
      await this.service.socket.init();
      this.ctx.body = {
        success: true,
        message: 'Socket.io 服务初始化成功'
      };
    } catch (error) {
      this.ctx.body = {
        success: false,
        message: 'Socket.io 服务初始化失败',
        error: error instanceof Error ? error.message : String(error)
      };
    }
  }

  public async status() {
    const io = this.service.socket.getIO();
    this.ctx.body = {
      success: true,
      connected: io !== null,
      message: io ? 'Socket.io 服务运行中' : 'Socket.io 服务未启动'
    };
  }
}
